/*
 * FreeRTOS Kernel V10.5.1
 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * SPDX-License-Identifier: MIT
 *
 */

/* Standard includes. */
#include <stdlib.h>

/* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
 * all the API functions to use the MPU wrappers.  That should only be done when
 * task.h is included from an application file. */
#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE

#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "timers.h"

#if (INCLUDE_xTimerPendFunctionCall == 1) && (configUSE_TIMERS == 0)
    #error configUSE_TIMERS must be set to 1 to make the xTimerPendFunctionCall() function available.
#endif

/* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
 * for the header files above, but not in this file, in order to generate the
 * correct privileged Vs unprivileged linkage and placement. */
#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e9021 !e961 !e750. */


/* This entire source file will be skipped if the application is not configured
 * to include software timer functionality.  This #if is closed at the very bottom
 * of this file.  If you want to include software timer functionality then ensure
 * configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */
#if (configUSE_TIMERS == 1)

/* Misc definitions. */
#define tmrNO_DELAY                    ((TickType_t)0U)
#define tmrMAX_TIME_BEFORE_OVERFLOW    ((TickType_t)-1)

/* The name assigned to the timer service task.  This can be overridden by
 * defining trmTIMER_SERVICE_TASK_NAME in FreeRTOSConfig.h. */
 #ifndef configTIMER_SERVICE_TASK_NAME
 #define configTIMER_SERVICE_TASK_NAME    "Tmr Svc"
 #endif

/* Bit definitions used in the ucStatus member of a timer structure. */
#define tmrSTATUS_IS_ACTIVE                  ((uint8_t)0x01)
 #define tmrSTATUS_IS_STATICALLY_ALLOCATED   ((uint8_t)0x02)
 #define tmrSTATUS_IS_AUTORELOAD             ((uint8_t)0x04)

/**
 * 定时器节点管理结构
 */
typedef struct tmrTimerControl
{
    const char * pcTimerName;                   /* 定时器节点名字 */
    ListItem_t xTimerListItem;                  /* 插入链表的节点信息 */
    TickType_t xTimerPeriodInTicks;             /* 定时器超时时钟数 */
    void * pvTimerID;                           /* 定时器ID */
    TimerCallbackFunction_t pxCallbackFunction; /* 超时后的回调函数 */
#if (configUSE_TRACE_FACILITY == 1)
    UBaseType_t uxTimerNumber;                  /* trace ID */
#endif
    uint8_t ucStatus;                           /* 定时器的状态 */
} xTIMER;

/* The old xTIMER name is maintained above then typedefed to the new Timer_t
 * name below to enable the use of older kernel aware debuggers. */
typedef xTIMER Timer_t;

/* The definition of messages that can be sent and received on the timer queue.
 * Two types of message can be queued - messages that manipulate a software timer,
 * and messages that request the execution of a non-timer related callback.  The
 * two message types are defined in two separate structures, xTimerParametersType
 * and xCallbackParametersType respectively.
 */
typedef struct tmrTimerParameters
{
    TickType_t xMessageValue; /*<< An optional value used by a subset of commands, for example, when changing the period of a timer. */
    Timer_t * pxTimer;        /*<< The timer to which the command will be applied. */
} TimerParameter_t;

typedef struct tmrCallbackParameters
{
    PendedFunction_t pxCallbackFunction; /* << The callback function to execute. */
    void * pvParameter1;                 /* << The value that will be used as the callback functions first parameter. */
    uint32_t ulParameter2;               /* << The value that will be used as the callback functions second parameter. */
} CallbackParameters_t;

/**
 * The structure that contains the two message types, along with an identifier
 * that is used to determine which message type is valid.
 * 向定时器队列发送消息管理结构
 */
typedef struct tmrTimerQueueMessage
{
    BaseType_t xMessageID; /*<< The command being sent to the timer service task. */
    union
    {
        TimerParameter_t xTimerParameters;
        /* Don't include xCallbackParameters if it is not going to be used as
         * it makes the structure (and therefore the timer queue) larger. */
        #if (INCLUDE_xTimerPendFunctionCall == 1)
            CallbackParameters_t xCallbackParameters;
        #endif /* INCLUDE_xTimerPendFunctionCall */
    } u;
} DaemonTaskMessage_t;

/* The list in which active timers are stored.  Timers are referenced in expire
 * time order, with the nearest expiry time at the front of the list.  Only the
 * timer service task is allowed to access these lists.
 * xActiveTimerList1 and xActiveTimerList2 could be at function scope but that
 * breaks some kernel aware debuggers, and debuggers that reply on removing the
 * static qualifier. */
PRIVILEGED_DATA static List_t xActiveTimerList1;
PRIVILEGED_DATA static List_t xActiveTimerList2;
PRIVILEGED_DATA static List_t * pxCurrentTimerList;
PRIVILEGED_DATA static List_t * pxOverflowTimerList;

/* A queue that is used to send commands to the timer service task. */
PRIVILEGED_DATA static QueueHandle_t xTimerQueue = NULL;
PRIVILEGED_DATA static TaskHandle_t xTimerTaskHandle = NULL;

/*
 * Initialise the infrastructure used by the timer service task if it has not
 * been initialised already.
 */
static void prvCheckForValidListAndQueue(void) PRIVILEGED_FUNCTION;

/*
 * The timer service task (daemon).  Timer functionality is controlled by this
 * task.  Other tasks communicate with the timer service task using the
 * xTimerQueue queue.
 */
static portTASK_FUNCTION_PROTO(prvTimerTask, pvParameters) PRIVILEGED_FUNCTION;

/*
 * Called by the timer service task to interpret and process a command it
 * received on the timer queue.
 */
static void prvProcessReceivedCommands(void) PRIVILEGED_FUNCTION;

/*
 * Insert the timer into either xActiveTimerList1, or xActiveTimerList2,
 * depending on if the expire time causes a timer counter overflow.
 */
static BaseType_t prvInsertTimerInActiveList(Timer_t * const pxTimer,
                                                  const TickType_t xNextExpiryTime,
                                                  const TickType_t xTimeNow,
                                                  const TickType_t xCommandTime) PRIVILEGED_FUNCTION;

/*
 * Reload the specified auto-reload timer.  If the reloading is backlogged,
 * clear the backlog, calling the callback for each additional reload.  When
 * this function returns, the next expiry time is after xTimeNow.
 */
static void prvReloadTimer(Timer_t * const pxTimer,
                                TickType_t xExpiredTime,
                                const TickType_t xTimeNow) PRIVILEGED_FUNCTION;

/*
 * An active timer has reached its expire time.  Reload the timer if it is an
 * auto-reload timer, then call its callback.
 */
static void prvProcessExpiredTimer(const TickType_t xNextExpireTime,
                                        const TickType_t xTimeNow) PRIVILEGED_FUNCTION;

/*
 * The tick count has overflowed.  Switch the timer lists after ensuring the
 * current timer list does not still reference some timers.
 */
static void prvSwitchTimerLists(void) PRIVILEGED_FUNCTION;

/*
 * Obtain the current tick count, setting *pxTimerListsWereSwitched to pdTRUE
 * if a tick count overflow occurred since prvSampleTimeNow() was last called.
 */
static TickType_t prvSampleTimeNow(BaseType_t * const pxTimerListsWereSwitched) PRIVILEGED_FUNCTION;

/*
 * If the timer list contains any active timers then return the expire time of
 * the timer that will expire first and set *pxListWasEmpty to false.  If the
 * timer list does not contain any timers then return 0 and set *pxListWasEmpty
 * to pdTRUE.
 */
static TickType_t prvGetNextExpireTime(BaseType_t * const pxListWasEmpty) PRIVILEGED_FUNCTION;

/*
 * If a timer has expired, process it.  Otherwise, block the timer service task
 * until either a timer does expire or a command is received.
 */
static void prvProcessTimerOrBlockTask(const TickType_t xNextExpireTime,
                                            BaseType_t xListWasEmpty) PRIVILEGED_FUNCTION;

/*
 * Called after a Timer_t structure has been allocated either statically or
 * dynamically to fill in the structure's members.
 */
static void prvInitialiseNewTimer(const char * const pcTimerName,
                                       const TickType_t xTimerPeriodInTicks,
                                       const BaseType_t xAutoReload,
                                       void * const pvTimerID,
                                       TimerCallbackFunction_t pxCallbackFunction,
                                       Timer_t * pxNewTimer) PRIVILEGED_FUNCTION;
/**
 *  创建处理软件定时器的任务
 */
BaseType_t xTimerCreateTimerTask(void)
{
    BaseType_t xReturn = pdFAIL;
    /* This function is called when the scheduler is started if
     * configUSE_TIMERS is set to 1.  Check that the infrastructure used by the
     * timer service task has been created/initialised.  If timers have already
     * been created then the initialisation will already have been performed. */
    prvCheckForValidListAndQueue();
    if (xTimerQueue != NULL)
    {

        /* 创建处理软件定时器的任务 */
        xReturn = xTaskCreate(prvTimerTask,
                                configTIMER_SERVICE_TASK_NAME,       /* 任务名称 */
                                configTIMER_TASK_STACK_DEPTH,        /* 堆栈大小 */
                                NULL,
                                ((UBaseType_t) configTIMER_TASK_PRIORITY) | portPRIVILEGE_BIT,   /* 优先级 */
                                &xTimerTaskHandle);


    }
    configASSERT(xReturn);
    return xReturn;
}


/**
 * 创建软件定时器
 * @xAutoReload: 是否为周期性定时器
 */
TimerHandle_t xTimerCreate(const char *const pcTimerName,
                                    const TickType_t xTimerPeriodInTicks,
                                    const BaseType_t xAutoReload,
                                    void * const pvTimerID,
                                    TimerCallbackFunction_t pxCallbackFunction)
{
    Timer_t * pxNewTimer;

    pxNewTimer = (Timer_t *)pvPortMalloc(sizeof(Timer_t));

    if (pxNewTimer != NULL)
    {
        /* Status is thus far zero as the timer is not created statically
         * and has not been started.  The auto-reload bit may get set in
         * prvInitialiseNewTimer. */
        pxNewTimer->ucStatus = 0x00;
        prvInitialiseNewTimer(pcTimerName,
                                xTimerPeriodInTicks,
                                xAutoReload,
                                pvTimerID,
                                pxCallbackFunction,
                                pxNewTimer);
    }

    return pxNewTimer;
}

/**
 * 设置软件定时器
 * @pcTimerName:            定期器的名字
 * @xTimerPeriodInTicks:    定时器超时时钟数
 * @xAutoReload:            是否为周期性定时器
 * @pxCallbackFunction:     定时器超时执行函数
 */
static void prvInitialiseNewTimer(const char * const pcTimerName,
                                   const TickType_t xTimerPeriodInTicks,
                                   const BaseType_t xAutoReload,
                                   void * const pvTimerID,
                                   TimerCallbackFunction_t pxCallbackFunction,
                                   Timer_t * pxNewTimer)
{
    configASSERT((xTimerPeriodInTicks > 0));

    prvCheckForValidListAndQueue();
    pxNewTimer->pcTimerName = pcTimerName;
    pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;
    pxNewTimer->pvTimerID = pvTimerID;
    pxNewTimer->pxCallbackFunction = pxCallbackFunction;
    vListInitialiseItem(&(pxNewTimer->xTimerListItem));
    if (xAutoReload != pdFALSE)
    {
        pxNewTimer->ucStatus |= tmrSTATUS_IS_AUTORELOAD;
    }
    traceTIMER_CREATE(pxNewTimer);
}

/**
 * 向软件定时器消息队列发送命令
 */
BaseType_t xTimerGenericCommand(TimerHandle_t xTimer,
                                const BaseType_t xCommandID,
                                const TickType_t xOptionalValue,
                                BaseType_t * const pxHigherPriorityTaskWoken,
                                const TickType_t xTicksToWait)
{
    BaseType_t xReturn = pdFAIL;
    DaemonTaskMessage_t xMessage;
    configASSERT(xTimer);
    /* Send a message to the timer service task to perform a particular action
     * on a particular timer definition. */
    if (xTimerQueue != NULL)
    {
        /* Send a command to the timer service task to start the xTimer timer. */
        xMessage.xMessageID = xCommandID;
        xMessage.u.xTimerParameters.xMessageValue = xOptionalValue;
        xMessage.u.xTimerParameters.pxTimer = xTimer;
        if (xCommandID < tmrFIRST_FROM_ISR_COMMAND)
        {
            if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING)
            {
                xReturn = xQueueSendToBack(xTimerQueue, &xMessage, xTicksToWait);
            }
            else
            {
                xReturn = xQueueSendToBack(xTimerQueue, &xMessage, tmrNO_DELAY);
            }
        }
        else
        {
            xReturn = xQueueSendToBackFromISR(xTimerQueue, &xMessage, pxHigherPriorityTaskWoken);
        }
        traceTIMER_COMMAND_SEND(xTimer, xCommandID, xOptionalValue, xReturn);
    }
    return xReturn;
}


TaskHandle_t xTimerGetTimerDaemonTaskHandle(void)
{
    /* If xTimerGetTimerDaemonTaskHandle() is called before the scheduler has been
     * started, then xTimerTaskHandle will be NULL.
     */
    configASSERT((xTimerTaskHandle != NULL));
    return xTimerTaskHandle;
}


TickType_t xTimerGetPeriod(TimerHandle_t xTimer)
{
    Timer_t * pxTimer = xTimer;
    configASSERT(xTimer);
    return pxTimer->xTimerPeriodInTicks;
}

/**
 * 设置timer为周期性触发定时器
 */
void vTimerSetReloadMode(TimerHandle_t xTimer, const BaseType_t xAutoReload)
{
    Timer_t * pxTimer = xTimer;
    configASSERT(xTimer);
    taskENTER_CRITICAL();
    {
        if (xAutoReload != pdFALSE)
        {
            pxTimer->ucStatus |= tmrSTATUS_IS_AUTORELOAD;
        }
        else
        {
            pxTimer->ucStatus &= ((uint8_t) ~tmrSTATUS_IS_AUTORELOAD);
        }
    }
    taskEXIT_CRITICAL();
}

/**
 * 判断定时器xTimer是否为周期性触发定时器
 */
BaseType_t xTimerGetReloadMode(TimerHandle_t xTimer)
{
    Timer_t * pxTimer = xTimer;
    BaseType_t xReturn;

    configASSERT(xTimer);
    taskENTER_CRITICAL();
    {
        if ((pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD) == 0)
        {
            /* Not an auto-reload timer. */
            xReturn = pdFALSE;
        }
        else
        {
            /* Is an auto-reload timer. */
            xReturn = pdTRUE;
        }
    }
    taskEXIT_CRITICAL();

    return xReturn;
}

UBaseType_t uxTimerGetReloadMode(TimerHandle_t xTimer)
{
    return (UBaseType_t)xTimerGetReloadMode(xTimer);
}


TickType_t xTimerGetExpiryTime(TimerHandle_t xTimer)
{
    Timer_t * pxTimer = xTimer;
    TickType_t xReturn;

    configASSERT(xTimer);
    xReturn = listGET_LIST_ITEM_VALUE(&(pxTimer->xTimerListItem));
    return xReturn;
}


const char * pcTimerGetName(TimerHandle_t xTimer)
{
    Timer_t * pxTimer = xTimer;

    configASSERT(xTimer);
    return pxTimer->pcTimerName;
}


static void prvReloadTimer(Timer_t * const pxTimer,
                            TickType_t xExpiredTime,
                            const TickType_t xTimeNow)
{
    /* Insert the timer into the appropriate list for the next expiry time.
     * If the next expiry time has already passed, advance the expiry time,
     * call the callback function, and try again. */
    while(prvInsertTimerInActiveList(pxTimer, (xExpiredTime + pxTimer->xTimerPeriodInTicks), xTimeNow, xExpiredTime) != pdFALSE)
    {
        /* Advance the expiry time. */
        xExpiredTime += pxTimer->xTimerPeriodInTicks;
        /* Call the timer callback. */
        traceTIMER_EXPIRED(pxTimer);
        pxTimer->pxCallbackFunction((TimerHandle_t)pxTimer);
    }
}


static void prvProcessExpiredTimer(const TickType_t xNextExpireTime,
                                    const TickType_t xTimeNow)
{
    Timer_t * const pxTimer = (Timer_t *)listGET_OWNER_OF_HEAD_ENTRY(pxCurrentTimerList);
    /* Remove the timer from the list of active timers.  A check has already
     * been performed to ensure the list is not empty. */
    (void)uxListRemove(&(pxTimer->xTimerListItem));
    /* If the timer is an auto-reload timer then calculate the next
     * expiry time and re-insert the timer in the list of active timers. */
    if ((pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD) != 0)
    {
        /* 周期性定时器 */
        prvReloadTimer(pxTimer, xNextExpireTime, xTimeNow);
    }
    else
    {
        /* oneshot类型定时器 */
        pxTimer->ucStatus &= ((uint8_t) ~tmrSTATUS_IS_ACTIVE);
    }

    /* Call the timer callback. */
    traceTIMER_EXPIRED(pxTimer);
    /* 调用定时器超时函数 */
    pxTimer->pxCallbackFunction((TimerHandle_t)pxTimer);
}


/**
 * 软件定时器任务处理入口
 */
static portTASK_FUNCTION(prvTimerTask, pvParameters)
{
    TickType_t xNextExpireTime;
    BaseType_t xListWasEmpty;
    /* Just to avoid compiler warnings. */
    (void) pvParameters;

    for( ; ; )
    {
        /**
         * Query the timers list to see if it contains any timers, and if so,
         * obtain the time at which the next timer will expire.
         * 获取下一个超时的定时器节点
         */
        xNextExpireTime = prvGetNextExpireTime(&xListWasEmpty);
        /**
         * If a timer has expired, process it.  Otherwise, block this task
         * until either a timer does expire, or a command is received.
         */
        prvProcessTimerOrBlockTask(xNextExpireTime, xListWasEmpty);
        /* Empty the command queue. */
        prvProcessReceivedCommands();
    }
}

static void prvProcessTimerOrBlockTask(const TickType_t xNextExpireTime,
                                        BaseType_t xListWasEmpty)
{
    TickType_t xTimeNow;
    BaseType_t xTimerListsWereSwitched;
    vTaskSuspendAll();
    {
        /* Obtain the time now to make an assessment as to whether the timer
         * has expired or not.  If obtaining the time causes the lists to switch
         * then don't process this timer as any timers that remained in the list
         * when the lists were switched will have been processed within the
         * prvSampleTimeNow() function. */
        xTimeNow = prvSampleTimeNow(&xTimerListsWereSwitched);
        if (xTimerListsWereSwitched == pdFALSE)
        {
            /* The tick count has not overflowed, has the timer expired? */
            if ((xListWasEmpty == pdFALSE) && (xNextExpireTime <= xTimeNow))
            {
                (void)xTaskResumeAll();
                prvProcessExpiredTimer(xNextExpireTime, xTimeNow);
            }
            else
            {
                /* The tick count has not overflowed, and the next expire
                 * time has not been reached yet.  This task should therefore
                 * block to wait for the next expire time or a command to be
                 * received - whichever comes first.  The following line cannot
                 * be reached unless xNextExpireTime > xTimeNow, except in the
                 * case when the current timer list is empty. */
                if (xListWasEmpty != pdFALSE)
                {
                    /* The current timer list is empty - is the overflow list
                     * also empty? */
                    xListWasEmpty = listLIST_IS_EMPTY(pxOverflowTimerList);
                }
                vQueueWaitForMessageRestricted(xTimerQueue, (xNextExpireTime - xTimeNow), xListWasEmpty);
                if (xTaskResumeAll() == pdFALSE)
                {
                    /* Yield to wait for either a command to arrive, or the
                     * block time to expire.  If a command arrived between the
                     * critical section being exited and this yield then the yield
                     * will not cause the task to block. */
                    portYIELD_WITHIN_API();
                }
            }
        }
        else
        {
            (void)xTaskResumeAll();
        }
    }
}

static TickType_t prvGetNextExpireTime(BaseType_t * const pxListWasEmpty)
{
    TickType_t xNextExpireTime;
    /* Timers are listed in expiry time order, with the head of the list
     * referencing the task that will expire first.  Obtain the time at which
     * the timer with the nearest expiry time will expire.  If there are no
     * active timers then just set the next expire time to 0.  That will cause
     * this task to unblock when the tick count overflows, at which point the
     * timer lists will be switched and the next expiry time can be
     * re-assessed.
     */
    *pxListWasEmpty = listLIST_IS_EMPTY(pxCurrentTimerList);
    /* pxCurrentTimerList队列不为空 */
    if (*pxListWasEmpty == pdFALSE)
    {
        /* 获取pxCurrentTimerList的头结点 */
        xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY(pxCurrentTimerList);
    }
    else
    {
        /**
         * Ensure the task unblocks when the tick count rolls over.
         * 队列为空，超时为0
         */
        xNextExpireTime = (TickType_t)0U;
    }
    return xNextExpireTime;
}


static TickType_t prvSampleTimeNow(BaseType_t * const pxTimerListsWereSwitched)
{
    TickType_t xTimeNow;
    PRIVILEGED_DATA static TickType_t xLastTime = (TickType_t)0U;
    xTimeNow = xTaskGetTickCount();
    if (xTimeNow < xLastTime)
    {
        prvSwitchTimerLists();
        *pxTimerListsWereSwitched = pdTRUE;
    }
    else
    {
        *pxTimerListsWereSwitched = pdFALSE;
    }
    xLastTime = xTimeNow;
    return xTimeNow;
}

/**
 * 定时器节点插入到定时队列中
 * @pxTimer
*/
static BaseType_t prvInsertTimerInActiveList(Timer_t * const pxTimer,
                                              const TickType_t xNextExpiryTime,
                                              const TickType_t xTimeNow,
                                              const TickType_t xCommandTime)
{
    BaseType_t xProcessTimerNow = pdFALSE;
    listSET_LIST_ITEM_VALUE(&(pxTimer->xTimerListItem ), xNextExpiryTime );
    listSET_LIST_ITEM_OWNER(&(pxTimer->xTimerListItem ), pxTimer );
    if (xNextExpiryTime <= xTimeNow)
    {
        /* Has the expiry time elapsed between the command to start/reset a
         * timer was issued, and the time the command was processed? */
        if (((TickType_t) (xTimeNow - xCommandTime)) >= pxTimer->xTimerPeriodInTicks)
        {
            /* The time between a command being issued and the command being
             * processed actually exceeds the timers period.  */
            xProcessTimerNow = pdTRUE;
        }
        else
        {
            vListInsert(pxOverflowTimerList, &(pxTimer->xTimerListItem));
        }
    }
    else
    {
        if ((xTimeNow < xCommandTime) && (xNextExpiryTime >= xCommandTime))
        {
            /* If, since the command was issued, the tick count has overflowed
             * but the expiry time has not, then the timer must have already passed
             * its expiry time and should be processed immediately. */
            xProcessTimerNow = pdTRUE;
        }
        else
        {
            vListInsert(pxCurrentTimerList, &(pxTimer->xTimerListItem));
        }
    }
    return xProcessTimerNow;
}

/* 处理定时器发送的消息 */
static void prvProcessReceivedCommands(void)
{
    DaemonTaskMessage_t xMessage;
    Timer_t * pxTimer;
    BaseType_t xTimerListsWereSwitched;
    TickType_t xTimeNow;

    while(xQueueReceive(xTimerQueue, &xMessage, tmrNO_DELAY) != pdFAIL)
    {
        #if (INCLUDE_xTimerPendFunctionCall == 1)
        {
            /* Negative commands are pended function calls rather than timer
             * commands. */
            if (xMessage.xMessageID < (BaseType_t)0)
            {
                const CallbackParameters_t * const pxCallback = &(xMessage.u.xCallbackParameters);
                /* The timer uses the xCallbackParameters member to request a
                 * callback be executed.  Check the callback is not NULL. */
                configASSERT(pxCallback);
                /* Call the function. */
                pxCallback->pxCallbackFunction(pxCallback->pvParameter1, pxCallback->ulParameter2);
            }
        }
        #endif /* INCLUDE_xTimerPendFunctionCall */
        /* Commands that are positive are timer commands rather than pended
         * function calls. */
        if (xMessage.xMessageID >= (BaseType_t)0)
        {
            /* The messages uses the xTimerParameters member to work on a
             * software timer.
             * 获得发送消息的定时器
             */
            pxTimer = xMessage.u.xTimerParameters.pxTimer;
            if (listIS_CONTAINED_WITHIN(NULL, &(pxTimer->xTimerListItem)) == pdFALSE)
            {
                /* The timer is in a list, remove it. */
                (void) uxListRemove(&(pxTimer->xTimerListItem));
            }

            traceTIMER_COMMAND_RECEIVED(pxTimer, xMessage.xMessageID, xMessage.u.xTimerParameters.xMessageValue);
            /* In this case the xTimerListsWereSwitched parameter is not used, but
             *  it must be present in the function call.  prvSampleTimeNow() must be
             *  called after the message is received from xTimerQueue so there is no
             *  possibility of a higher priority task adding a message to the message
             *  queue with a time that is ahead of the timer daemon task (because it
             *  pre-empted the timer daemon task after the xTimeNow value was set). */
            xTimeNow = prvSampleTimeNow(&xTimerListsWereSwitched);
            switch(xMessage.xMessageID)
            {
                case tmrCOMMAND_START:
                case tmrCOMMAND_START_FROM_ISR:
                case tmrCOMMAND_RESET:
                case tmrCOMMAND_RESET_FROM_ISR:
                    /**
                     * Start or restart a timer.
                     * 启动定时器
                     */
                    pxTimer->ucStatus |= tmrSTATUS_IS_ACTIVE;
                    if (prvInsertTimerInActiveList(pxTimer,
                            xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks,
                            xTimeNow,
                            xMessage.u.xTimerParameters.xMessageValue) != pdFALSE)
                    {
                        /* The timer expired before it was added to the active
                         * timer list.  Process it now. */
                        if ((pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD) != 0)
                        {
                            prvReloadTimer(pxTimer, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow);
                        }
                        else
                        {
                            pxTimer->ucStatus &= ((uint8_t ) ~tmrSTATUS_IS_ACTIVE);
                        }
                        /* Call the timer callback. */
                        traceTIMER_EXPIRED(pxTimer);
                        pxTimer->pxCallbackFunction((TimerHandle_t)pxTimer);
                    }
                    break;
                case tmrCOMMAND_STOP:
                case tmrCOMMAND_STOP_FROM_ISR:
                    /* The timer has already been removed from the active list. */
                    pxTimer->ucStatus &= ((uint8_t ) ~tmrSTATUS_IS_ACTIVE);
                    break;
                case tmrCOMMAND_CHANGE_PERIOD:
                case tmrCOMMAND_CHANGE_PERIOD_FROM_ISR:
                    pxTimer->ucStatus |= tmrSTATUS_IS_ACTIVE;
                    pxTimer->xTimerPeriodInTicks = xMessage.u.xTimerParameters.xMessageValue;
                    configASSERT((pxTimer->xTimerPeriodInTicks > 0));
                    /* The new period does not really have a reference, and can
                     * be longer or shorter than the old one.  The command time is
                     * therefore set to the current time, and as the period cannot
                     * be zero the next expiry time can only be in the future,
                     * meaning (unlike for the xTimerStart() case above) there is
                     * no fail case that needs to be handled here. */
                    (void) prvInsertTimerInActiveList(pxTimer, (xTimeNow + pxTimer->xTimerPeriodInTicks), xTimeNow, xTimeNow);
                    break;
                case tmrCOMMAND_DELETE:
                    /* The timer has already been removed from the active list,
                     * just free up the memory if the memory was dynamically
                     * allocated. */
                    if ((pxTimer->ucStatus & tmrSTATUS_IS_STATICALLY_ALLOCATED) == (uint8_t)0)
                    {
                        vPortFree(pxTimer);
                    }
                    else
                    {
                        pxTimer->ucStatus &= ((uint8_t) ~tmrSTATUS_IS_ACTIVE);
                    }
                    break;
                default:
                    /* Don't expect to get here. */
                    break;
            }
        }
    }
}

/**
 * 交换两个定时器队列
 */
static void prvSwitchTimerLists(void)
{
    TickType_t xNextExpireTime;
    List_t * pxTemp;
    /* The tick count has overflowed.  The timer lists must be switched.
     * If there are any timers still referenced from the current timer list
     * then they must have expired and should be processed before the lists
     * are switched. */
    while(listLIST_IS_EMPTY(pxCurrentTimerList) == pdFALSE)
    {
        xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY(pxCurrentTimerList);
        /* Process the expired timer.  For auto-reload timers, be careful to
         * process only expirations that occur on the current list.  Further
         * expirations must wait until after the lists are switched. */
        prvProcessExpiredTimer(xNextExpireTime, tmrMAX_TIME_BEFORE_OVERFLOW);
    }
    pxTemp = pxCurrentTimerList;
    pxCurrentTimerList = pxOverflowTimerList;
    pxOverflowTimerList = pxTemp;
}

static void prvCheckForValidListAndQueue(void)
{
    /* Check that the list from which active timers are referenced, and the
     * queue used to communicate with the timer service, have been
     * initialised.
     */
    taskENTER_CRITICAL();
    {
        /* 第一次创建定时器的时候，会创建通信队列 */
        if (xTimerQueue == NULL)
        {
            vListInitialise(&xActiveTimerList1);
            vListInitialise(&xActiveTimerList2);
            pxCurrentTimerList = &xActiveTimerList1;
            pxOverflowTimerList = &xActiveTimerList2;
            /* 创建与处理软件定时器任务通信的队列 */
            xTimerQueue = xQueueCreate((UBaseType_t)configTIMER_QUEUE_LENGTH, sizeof(DaemonTaskMessage_t));

            #if (configQUEUE_REGISTRY_SIZE > 0)
            {
                if (xTimerQueue != NULL)
                {
                    vQueueAddToRegistry(xTimerQueue, "TmrQ");
                }
            }
            #endif /* configQUEUE_REGISTRY_SIZE */
        }
    }
    taskEXIT_CRITICAL();
}

BaseType_t xTimerIsTimerActive(TimerHandle_t xTimer)
{
    BaseType_t xReturn;
    Timer_t * pxTimer = xTimer;
    configASSERT(xTimer);
    /* Is the timer in the list of active timers? */
    taskENTER_CRITICAL();
    {
        if ((pxTimer->ucStatus & tmrSTATUS_IS_ACTIVE) == 0)
        {
            xReturn = pdFALSE;
        }
        else
        {
            xReturn = pdTRUE;
        }
    }
    taskEXIT_CRITICAL();
    return xReturn;
}


void * pvTimerGetTimerID(const TimerHandle_t xTimer)
{
    Timer_t * const pxTimer = xTimer;
    void * pvReturn;
    configASSERT(xTimer);
    taskENTER_CRITICAL();
    {
        pvReturn = pxTimer->pvTimerID;
    }
    taskEXIT_CRITICAL();
    return pvReturn;
}

void vTimerSetTimerID(TimerHandle_t xTimer, void * pvNewID)
{
    Timer_t * const pxTimer = xTimer;
    configASSERT(xTimer);
    taskENTER_CRITICAL();
    {
        pxTimer->pvTimerID = pvNewID;
    }
    taskEXIT_CRITICAL();
}

#if (INCLUDE_xTimerPendFunctionCall == 1)
BaseType_t xTimerPendFunctionCallFromISR(PendedFunction_t xFunctionToPend,
                                          void * pvParameter1,
                                          uint32_t ulParameter2,
                                          BaseType_t * pxHigherPriorityTaskWoken)
{
    DaemonTaskMessage_t xMessage;
    BaseType_t xReturn;
    /* Complete the message with the function parameters and post it to the
     * daemon task. */
    xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK_FROM_ISR;
    xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;
    xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;
    xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;
    xReturn = xQueueSendFromISR(xTimerQueue, &xMessage, pxHigherPriorityTaskWoken);
    tracePEND_FUNC_CALL_FROM_ISR(xFunctionToPend, pvParameter1, ulParameter2, xReturn);
    return xReturn;
}
BaseType_t xTimerPendFunctionCall(PendedFunction_t xFunctionToPend,
                                       void * pvParameter1,
                                       uint32_t ulParameter2,
                                       TickType_t xTicksToWait)
{
    DaemonTaskMessage_t xMessage;
    BaseType_t xReturn;
    /* This function can only be called after a timer has been created or
     * after the scheduler has been started because, until then, the timer
     * queue does not exist. */
    configASSERT(xTimerQueue);
    /* Complete the message with the function parameters and post it to the
     * daemon task. */
    xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK;
    xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;
    xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;
    xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;
    xReturn = xQueueSendToBack(xTimerQueue, &xMessage, xTicksToWait);
    tracePEND_FUNC_CALL(xFunctionToPend, pvParameter1, ulParameter2, xReturn);
    return xReturn;
}

#endif


#if (configUSE_TRACE_FACILITY == 1)

UBaseType_t uxTimerGetTimerNumber(TimerHandle_t xTimer)
{
    return ((Timer_t *)xTimer)->uxTimerNumber;
}

void vTimerSetTimerNumber(TimerHandle_t xTimer,
                                   UBaseType_t uxTimerNumber)
{
    ((Timer_t *)xTimer)->uxTimerNumber = uxTimerNumber;
}

#endif

#endif /* configUSE_TIMERS == 1 */
